package com.study.demo.ui.splash;

import android.content.Context;
import android.support.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.stduy.injector.PerActivity;
import com.study.demo.Constants;
import com.study.demo.bean.UpdateInfo;
import com.study.demo.components.okhttp.OkHttpHelper;
import com.study.demo.util.ChannelUtil;
import com.study.demo.util.SettingPrefUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by sll on 2016/5/31.
 */
@PerActivity
public class SplashPresenter implements SplashContract.Presenter {

    private SplashContract.View mSplashView;
    private Disposable disposable;

    private Context mContext;
    private OkHttpHelper mOkHttpHelper;

    @Inject
    public SplashPresenter(Context mContext, OkHttpHelper mOkHttpHelper) {
        this.mContext = mContext;
        this.mOkHttpHelper = mOkHttpHelper;
    }

    @Override
    public void initUmeng() {
        MobclickAgent.UMAnalyticsConfig config =
                new MobclickAgent.UMAnalyticsConfig(mContext, "55f1993be0f55a0fd9004fbc",
                        ChannelUtil.getChannel(mContext));
        MobclickAgent.startWithConfigure(config);
    }

    @Override
    public void initHuPuSign() {
        disposable = Observable.create(
                (ObservableEmitter<UpdateInfo> observableEmitter) -> {
                    try {
                        String result = mOkHttpHelper.getStringFromServer(Constants.UPDATE_URL);
                        observableEmitter.onNext(JSON.parseObject(result, UpdateInfo.class));
                    } catch (Exception e) {
                        observableEmitter.onError(e);
                    }
                    observableEmitter.onComplete();
                })
                .timeout(5, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(updateInfo -> {
                            if (updateInfo != null) {
                                if (updateInfo.extra != null) {
                                    SettingPrefUtil.setNeedExam(mContext, updateInfo.extra.needExam == 1);
                                }
                                SettingPrefUtil.setHuPuSign(mContext, updateInfo.hupuSign);
                            }
                            mSplashView.showMainUi();
                        }, throwable -> {
                            throwable.printStackTrace();
                            mSplashView.showMainUi();
                        }
                );
    }

    @Override
    public void attachView(@NonNull SplashContract.View view) {
        mSplashView = view;
    }

    @Override
    public void detachView() {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
        mSplashView = null;
    }
}
